﻿using System.Collections.Generic;
using System.IO;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace System.Web.Mvc {
	public static class HtmlHelperExtensions {
//#if DEBUG
//        private const string StaticFileIsUnCompressed = ".uncompressed";
//        //private const string StaticFileServerRoot = "http://static-iwapz-com.googlecode.com/svn/trunk/";
//        private const string StaticFileServerRoot = "http://localhost/iwapz-static/";
//#else
//        private const string StaticFileServerRoot = "http://static.iwapz.com/";
//#endif

		/// <summary>
		/// Renders a favicon link tag.  Points to ~/favicon.ico.
		/// </summary>
		/// <param name="helper"></param>
		/// <returns></returns>
		[Obsolete]
		public static string Favicon(this HtmlHelper helper) {
			return string.Format("<link rel=\"shortcut icon\" href=\"{0}\" />\n", helper.ResolveUrl("~/favicon.ico"));
		}

		/// <summary>
		/// Normalizes a url in the form ~/path/to/resource.aspx.
		/// </summary>
		/// <param name="helper"></param>
		/// <param name="relativeUrl"></param>
		/// <returns></returns>
		public static string ResolveUrl(this HtmlHelper helper, string relativeUrl) {
			if (relativeUrl == null)
				return null;

			if (!relativeUrl.StartsWith("~"))
				return relativeUrl;

			return (helper.ViewContext.HttpContext.Request.ApplicationPath + relativeUrl.Substring(1)).Replace("//", "/");
		}

		public static IGridView<T> GridView<T>(this HtmlHelper helper, IEnumerable<T> dataSource) where T : class {
			return new GridView<T>(dataSource, helper.ViewContext.HttpContext.Response.Output, helper.ViewContext);
		}

		#region GoogleAnalytics
		
		[Obsolete]
		private const string GoogleAnalyticsJavaScript = @"
        <script type=""text/javascript"">
        /* Google-Analytics-Code */
        var gaJsHost = ((""https:"" == document.location.protocol) ? ""https://ssl."" : ""http://www."");
        document.write(unescape(""%3Cscript src='"" + gaJsHost + ""google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E""));
        </script>
        <script type=""text/javascript"">
        var pageTracker = _gat._getTracker(""{0}"");{1}		
        pageTracker._initData();
        pageTracker._trackPageview();
        /* Google-Analytics-Code */
        </script>";

		[Obsolete]
		public static String GoogleAnalytics(this HtmlHelper helper, string googleAnalyticsKey) {
			return helper.GoogleAnalytics(googleAnalyticsKey, null);
		}

		[Obsolete]
		public static String GoogleAnalytics(this HtmlHelper helper, string googleAnalyticsKey, string domain)
		{
			const string domainJs = @"
		pageTracker._setDomainName(""{0}"");";

			return string.Format(
				GoogleAnalyticsJavaScript, 
				googleAnalyticsKey, 
				String.IsNullOrEmpty(domain) ? "" : String.Format(domainJs, domain)
				);
		}
		#endregion

		//        public static string GoogleLoad(this HtmlHelper helper, string name, string versions) {
//            return helper.GoogleLoad(name, versions, null);
//        }

//        public static string GoogleLoad(this HtmlHelper helper, string name, string versions, object extras) {
//            return helper.GoogleLoad(name, versions, new RouteValueDictionary(extras));
//        }

//        private static string GoogleLoad(this HtmlHelper helper, string name, string versions, RouteValueDictionary extras) {
//            //var page = helper.ViewDataContainer as Page;
//            //page.ClientScript.IsClientScriptIncludeRegistered("asd");
//            if (HttpContext.Current.IsDebuggingEnabled) {
//                if (extras == null)
//                    extras = new RouteValueDictionary();

//                if (!extras.ContainsKey("uncompressed"))
//                    extras.Add("uncompressed", true);
//            }

//            string options = string.Empty;
//            if (extras != null) {
//                foreach (var keyValuePair in extras) {
//                    // TODO:GoogleLoad
//                }
//            }
//            return string.Format("google.load(\"{0}\", \"{1}\"{2});", name, versions, "");
//        }

//        //public static string IncludeCommonStaticFiles(this HtmlHelper helper) {
//        //    return helper.IncludeStaticFiles("/reset.css", "/typography.css", "/layout.css", "/font.css", "$jquery/jquery.js", "$jquery/jquery-ui.js");
//        //}

//        public static string UseShareStyleSheets(this HtmlHelper helper)
//        {
//            return IncludeStaticFiles("/reset.css", "/typography.css", "/layout.css", "/font.css");
//        }

//        public static string UsejQueryUI(this HtmlHelper helper) {
//            return IncludeStaticFiles("$jquery/jquery.js", "$jquery/jquery-ui.js");
//        }

//        public static string UsejQueryUI(this HtmlHelper helper, string theme)
//        {
//            return IncludeStaticFiles("$jquery/jquery.js", "$jquery/jquery-ui.js", string.Format("$jquery/ui-themes/{0}/theme.css", theme));
//        }

//        public static string UseApplicationFiles(this HtmlHelper helper, params string[] files)
//        {
//            for (var i = 0; i < files.Length; i++) {
//                files[i] = "~/" + files[i];
//            }
//            return IncludeStaticFiles(files);
//        }

//        private static string IncludeStaticFiles(params string[] files) {
//            string application = HttpContext.Current.Application.GetRequiredValue<string>("app_static_server_folder");

//            StringBuilder sb = new StringBuilder();
//            for (var i = 0; i < files.Length; i++) {
//                var file = files[i];

//                var extension = Path.GetExtension(file);
//#if DEBUG
//                file = file.Remove(file.Length - extension.Length, extension.Length) + StaticFileIsUnCompressed + extension;
//#endif
//                if (file.StartsWith("/"))
//                    file = "share" + file;
//                else if (file.StartsWith("$"))
//                    file = file.Remove(0, 1);
//                else if (file.StartsWith("~/"))
//                    file = application + file.Remove(0, 1);

//                if (extension.Equals(".js", StringComparison.CurrentCultureIgnoreCase))
//                    sb.AppendFormat("<script type=\"text/javascript\" src=\"{0}{1}\" ></script>\n", StaticFileServerRoot, file);
//                else if (extension.Equals(".css", StringComparison.CurrentCultureIgnoreCase))
//                    sb.AppendFormat("<link type=\"text/css\" rel=\"stylesheet\" href=\"{0}{1}\" />\n", StaticFileServerRoot, file);
//            }

//            return sb.ToString();
//        }
	}
}